package com.maslyk.invitation.entitybean;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.maslyk.invitation.audit.ObjectName;
import com.maslyk.invitation.audit.PropertyLabel;
import com.maslyk.invitation.entitybean.abstrct.GenericEntity;
import com.maslyk.invitation.enums.OrderPositionTypeEnum;

/**
 *
 * @author kris
 */
@Entity
@Table(name = "insertion")
@ObjectName(label="Wkładka")
public class Insertion  implements GenericEntity, IOrderPosition
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;
    @Column(name = "totalcount")
    @PropertyLabel(label="Ilość")
    private int totalcount;
    @Column(name = "overPrintCount")
    @PropertyLabel(label="Ilość z nadrukiem")
    private int overPrintCount;
    @Column(name = "code")
    @PropertyLabel(label="Kod")
    private String code;
    @Column(name = "unitprice")
    @PropertyLabel(label="Cena")
    private Double unitprice = new Double(0.0);
    @Column(name = "note")
    @PropertyLabel(label="Uwagi")
    private String note;
    @JoinColumn(name = "orderid", referencedColumnName = "id")
    @ManyToOne(optional = false, fetch = FetchType.LAZY, targetEntity=Order.class)
    private Order order;

    @Transient
    private StringBuffer calculatingNote = new StringBuffer();

    public Insertion()
    {
    }

    public Insertion(Integer id)
    {
        this.id = id;
    }

    public Insertion(Integer id, int totalcount)
    {
        this.id = id;
        this.totalcount = totalcount;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public int getTotalcount()
    {
        return totalcount;
    }

    public void setTotalcount(int totalcount)
    {
        this.totalcount = totalcount;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode(String code)
    {
        this.code = code;
    }

    public Double getUnitprice()
    {
        return unitprice;
    }

    public void setUnitprice(Double unitprice)
    {
        this.unitprice = unitprice;
    }

    public Order getOrder()
    {
        return order;
    }

    public void setOrder(Order order)
    {
        this.order = order;
    }

    public String getNote()
    {
        return note;
    }

    public void setNote(String note)
    {
        this.note = note;
    }

    public int getOverPrintCount()
    {
        return overPrintCount;
    }

    public void setOverPrintCount(int overPrintCount)
    {
        this.overPrintCount = overPrintCount;
    }

    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }
    public Double getAmount()
    {
        double totalAmount = 0.0d;
        calculatingNote = new StringBuffer();
        
        totalAmount += totalcount * unitprice.doubleValue();
        calculatingNote.append("Kwota za wkladki: "+totalcount+" * "+unitprice.doubleValue()+" = "+totalAmount+"<br>");
        totalAmount += overPrintCount * 0.50;
        calculatingNote.append("Kwota za nadruk: "+overPrintCount+" * 0.50<br>");
        
        BigDecimal amount = new BigDecimal(totalAmount);
        amount.setScale(2,BigDecimal.ROUND_HALF_UP);
        return amount.doubleValue();
    }

    public StringBuffer getCalculatingNote()
    {
        return calculatingNote;
    }

    public OrderPositionTypeEnum getType()
    {
        return OrderPositionTypeEnum.INSERTION;
    }
    
	public static String getBeanName()
	{
		return "insertionBean";
	}

	public String getKey() {
		return getOrder().getNumber();
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		Insertion ins = (Insertion) super.clone();
		ins.setId(null);
		ins.setOrder(null);
		return ins;
	}
	
	public boolean isInserted() {
		return id != null;
	}
}
